﻿using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoreLims.Models.CoreLims
{
    using SampleGroup = SampleSampleGroup;

    public static class SampleSampleGroupExtension
    {
        public static List<SampleGroup> getSampleGroupList(this SampleGroup sg, CoreLimsContext dbContext) => dbContext.SampleGroup.ToList();

        public static SampleGroup getSampleGroupById(this SampleGroup sg, CoreLimsContext dbContext, string Id) =>
            dbContext.SampleGroup.FirstOrDefault(m => m.Id == Id);

        /// <summary>
        /// 根据 样品组类型 获取样品组数据
        /// </summary>
        /// <param name="Classification"></param>
        /// <returns></returns>
        public static List<SampleGroup> getSampleGroupListByType(this SampleGroup sg, CoreLimsContext dbContext, string Type) =>
            dbContext.SampleGroup.Where(s => s.Type == Type).OrderBy(x => x.Name).ToList();

        /// <summary>
        /// 根据样品组ID(SampleGroupId),获得组内样品列表
        /// </summary>
        /// <param name="sg"></param>
        /// <param name="dbContext"></param>
        /// <param name="SampleGroupId"></param>
        /// <returns></returns>
        public static IQueryable<object> getSampleGroupBySGID(this SampleGroup sg, CoreLimsContext dbContext, string SampleGroupId)
        {
            var list = from sgroup in dbContext.SampleGroup
                        join sample in dbContext.SampleDefinition on sgroup.SampleId equals sample.Id
                        where (sgroup.SampleGroupId == SampleGroupId)
                        orderby (sample.Name)
                        select new
                        {
                            sgroup.Id,
                            sample.Name,
                            sample.DefaultNumber,
                            sample.Description,
                            sample.Site,
                            sample.Group,
                            sample.WorkFlowName,
                            sample.ReportName,
                            sgroup.SampleNumber,
                            sgroup.SampleId,
                        };
            return list;
        }

        /// <summary>
        /// 根据样品组ID(SampleGroupId),获得组内样品定义ID和数量字符串
        /// </summary>
        /// <param name="sg"></param>
        /// <param name="dbContext"></param>
        /// <param name="SampleGroupId"></param>
        /// <returns></returns>
        public static string getSampleStBySGID(this SampleGroup sg, CoreLimsContext dbContext, string SampleGroupId)
        {
            var list = dbContext.SampleGroup.Where(s => s.SampleGroupId == SampleGroupId).ToList();
            StringBuilder sb = new StringBuilder();
            foreach (var sample in list)
            {
                sb.Append(sample.SampleId + "." + sample.SampleNumber.ToString() + ",");
            }
            return sb.ToString();
        }

        /// <summary>
        /// 根据 样品组名称 模糊查询
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public static List<SampleGroup> getSampleGroupByName(this SampleGroup sg, CoreLimsContext dbContext, string Name) =>
            dbContext.SampleGroup.Where(m => m.Name.Contains(Name)).ToList().OrderBy(x => x.Name).ToList();

        /// <summary>
        /// 添加样品组
        /// </summary>
        /// <param name="sg"></param>
        /// <param name="dbContext"></param>
        /// <returns></returns>
        public static string AddSampleGroup(this SampleGroup sg, CoreLimsContext dbContext)
        {
            sg.Id = SAMPLE._newId();
            dbContext.Add(sg);
            dbContext.SaveChanges();
            return "样品组添加成功！";
        }

        /// <summary>
        /// 给样品组中添加样品
        /// </summary>
        /// <param name="sg"></param>
        /// <param name="dbContext"></param>
        /// <param name="SampleId"></param>
        /// <returns></returns>
        public static string AddSample(this SampleGroup sg, CoreLimsContext dbContext, string SampleId)
        {
            var temp = new SampleGroup();
            temp.SampleGroupId = sg.Id;
            temp.SampleId = SampleId;

            dbContext.Add(temp);
            dbContext.SaveChanges();
            return "样品添加成功！";
        }

        /// <summary>
        /// 更新样品组
        /// </summary>
        /// <param name="sg"></param>
        /// <param name="dbContext"></param>
        /// <returns></returns>
        public static string UpdateSampleGroup(this SampleGroup sg, CoreLimsContext dbContext)
        {
            SampleGroup sampleGroup = SAMPLE.SampleGroup.getSampleGroupById(dbContext, sg.Id);
            sampleGroup.Copy(sg);

            dbContext.Update(sampleGroup);
            dbContext.SaveChanges();
            return "样品组更新成功！";
        }

        /// <summary>
        /// 更新样品数量
        /// </summary>
        /// <param name="sg"></param>
        /// <param name="dbContext"></param>
        /// <param name="Id"></param>
        /// <param name="number"></param>
        /// <returns></returns>
        public static string UpdateSampleGroupNumber(this SampleGroup sg, CoreLimsContext dbContext, string Id, string number)
        {
            SampleGroup sampleGroup = SAMPLE.SampleGroup.getSampleGroupById(dbContext, Id);
            int snumber = 1;
            int.TryParse(number, out snumber);
            sampleGroup.SampleNumber = snumber;

            dbContext.Update(sampleGroup);
            dbContext.SaveChanges();
            return "样品组更新成功！";
        }

        /// <summary>
        /// 删除样品组定义
        /// </summary>
        /// <param name="sg"></param>
        /// <param name="dbContext"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static string DeleteSampleGroup(this SampleGroup sg, CoreLimsContext dbContext, string Id)
        {
            SampleGroup sampleGroup = SAMPLE.SampleGroup.getSampleGroupById(dbContext, Id);
            var list = SAMPLE.SampleGroup.getSampleGroupBySGID(dbContext, sampleGroup.Id);

            if (list.Count() > 0)
                return "请先删除样品组内的样品！";

            dbContext.Remove(sampleGroup);
            dbContext.SaveChanges();
            return "样品组删除成功！";
        }

        /// <summary>
        /// 删除样品组中样品
        /// </summary>
        /// <param name="sg"></param>
        /// <param name="dbContext"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static string DeleteSGSample(this SampleGroup sg, CoreLimsContext dbContext, string Id)
        {
            SampleGroup sampleGroup = SAMPLE.SampleGroup.getSampleGroupById(dbContext, Id);

            dbContext.Remove(sampleGroup);
            dbContext.SaveChanges();
            return "样品删除成功！";
        }

        /// <summary>
        /// 删除样品组中的样品
        /// </summary>
        /// <param name="sg"></param>
        /// <param name="dbContext"></param>
        /// <returns></returns>
        public static string DeleteSample(this SampleGroup sg, CoreLimsContext dbContext)
        {
            SampleGroup sampleGroup = SAMPLE.SampleGroup.getSampleGroupById(dbContext, sg.Id);

            dbContext.Remove(sampleGroup);
            dbContext.SaveChanges();
            return "样品删除成功！";
        }

        /// <summary>
        /// 根据样品组IDS，获取所有样品组内样品定义和数量的字符串用来进行样品登录
        /// </summary>
        /// <param name="sg"></param>
        /// <param name="dbContext"></param>
        /// <param name="GroupIds"></param>
        /// <returns></returns>
        public static string GetSampleDefinitionList(this SampleGroup sg, CoreLimsContext dbContext, string GroupIds)
        {
            if (GroupIds == null || GroupIds.Length == 0) return null;

            StringBuilder reList = new StringBuilder();
            var groupList = GroupIds.Split(',');
            foreach (var gid in groupList)
            {
                var sampleList = SAMPLE.SampleGroup.getSampleStBySGID(dbContext, gid);
                reList.Append(sampleList);
            }
            return reList.ToString();
        }

    }
}
